#!/bin/csh -f

# Read or write a tape, file by file
#
# usage: taperw [-f|-t|-v] tape-device output-file-base-name
#
# options:  -f specify "from tape"
#           -t specify "to tape"
#           -v specify "verify tape with file"

set direction="-f"
if ("$1" == "-f" || "$1" == "-t" || "$1" == "-v") then
    set direction=$1
    shift
endif

if ("$direction" == "-v") then
    onintr INTR
endif

set tape=$1
if ("$tape" == "") set tape=/dev/nrst8
set base=$2
if ("$base" == "") set base=tape

set one=1
set ten=0
set size=(1 2)

while ($size[1] != 0)

    set file=${base}.${ten}${one}
    if ("$direction" == "-f") then
        cp $tape $file
    else if ("$direction" == "-t") then
        cp $file $tape
    else # direction == "-v" (verify)
        cp $tape /tmp/tape.$$
	echo -n "comparing $file : "
        diff -h $file /tmp/tape.$$ >& /dev/null
	set result=$status
	if ($result == 0) then
	    echo "ok"
	else if ($result == 1) then
	    echo "differ"
	else # result == 2
	    echo "problem"
	endif
	/bin/rm -f /tmp/tape.$$
    endif

#    if ("$direction" != "-v") then
	set size=`ls -s $file`
	echo File $ten$one : $size[1] units
#    endif

    @ one = $one + 1
    if ($one > 9) then
	set one=0
	@ ten = $ten + 1
	if ($ten > 9) then
	    echo Too many files
	    exit 1
	endif
    endif

end

exit 0

INTR:
/bin/rm -f /tmp/tape.$$
exit 1

